package com.jinhongone.context.support;

import com.jinhongone.beans.factory.support.BeanDefinitionReader;
import com.jinhongone.beans.factory.support.BeanDefinitionRegistry;
import com.jinhongone.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jinho
 * @className AbstractApplicationContext
 * @description 实现ApplicationContext接口, 声明bean对象集合，存放bean对象
 * @date 2025/4/25 17:20
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    //聚合beanDefinition解析器，用于解析xml文件并获取注册表
    protected BeanDefinitionReader beanDefinitionReader;
    //定义bean对象存放容器(ioc容器)
    protected Map<String, Object> singletonObjects = new HashMap<>();
    //配置文件位置
    protected String configLocation;

    public void refresh() throws Exception {
        //解析xml配置文件
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //通过注册表，初始化bean
        finishBeanInitialization();
    }

    //bean的初始化
    private void finishBeanInitialization() throws Exception {
        //获取beanDefinitin的注册表
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //根据注册表信息，初始化bean
        String[] beanDefintionNames = registry.getBeanDefintionNames();
        for (String beanDefinitionName : beanDefintionNames) {
            //此处调用getBean()方法属于模板模式，该方法通过模块继承得来
            getBean(beanDefinitionName);
        }
    }

}
